/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NannyManager.java
 *
 * Created on Aug 11, 2010, 5:02:42 AM
 */

package GUI;
import config.Database_function;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author QUOCTHGC60050
 */
public class NannyManager extends javax.swing.JPanel {

    /** Creates new form NannyManager */

    Connection con= Database_function.setConnection();
    Statement st;
    ResultSet re;
    Vector table = new Vector();
    Vector label= new Vector();

    public NannyManager() {
        initComponents();
        this.jPanelAdvancedSearch.setVisible(false);
        this.setSize(830, 440);
        initTableN();
    }

    public void addTableN()
    {
        try
        {
            Vector temp= new Vector();

            temp.add(re.getString("NannyID"));
            temp.add(re.getString("FirstName"));
            temp.add(re.getString("LastName"));
            temp.add(re.getString("Birthday"));
            temp.add(re.getString("Gender"));
            temp.add(re.getString("Charge"));
            temp.add(re.getString("Experiences"));

            table.add(temp);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void initTableN()
    {
        try
        {
            st= con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
            re=st.executeQuery("select * from tbl_Nanny order by 'NannyID' asc");

            label.add("ID");label.add("FirstName");label.add("LastName");label.add("Birthday");label.add("Gender");label.add("Charge");label.add("Experiences");

           while(re.next())
            {
                addTableN();
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public void SearchN()
    {

        String searchString = jTextFieldSearch.getText();
        String searchType =(String) jComboBoxSearch.getSelectedItem();


        try
        {
            table.removeAllElements();

            if (searchType.equals("NannyID"))
            {
                re=st.executeQuery("select * from tbl_Nanny where NannyID like '%"+searchString+"%'");
                while(re.next())
                {
                    addTableN();
                }
            }
            else if (searchType.equals("Name"))
            {
                re=st.executeQuery("select * from tbl_Nanny where (LastName like '%"+searchString+"%') or (FirstName like '%"+searchString+"%') order by DIFFERENCE (Firstname, '"+searchString+"') desc");
                while(re.next())
                {
                    addTableN();
                }
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

    public void advancedSearchN()
    {
        String searchString = jTextFieldSearch.getText();
        String Gender= (String) jComboBoxGender.getSelectedItem();
        String Experiences = jTextFieldExperience.getText();
        String Charge = jTextFieldCharge.getText();
        String ChargeType = (String) jComboBoxCharge.getSelectedItem();
        String AgeType = (String) jComboBoxAge.getSelectedItem();
        String Age = jTextFieldAge.getText();
        String Query="select * from tbl_Nanny";
        table.removeAllElements();
        try
        {
            Query+=" where ((LastName like '"+searchString+"%') or (FirstName like '%"+searchString+"%'))";

            if (!Experiences.equals(""))
                Query+=" and (Experiences >="+Experiences+")";

            if (!Gender.equals("Not Important"))
                Query+="and (Gender like '"+Gender+"')";

            if (!Charge.equals(""))
                if (ChargeType.equals("at least"))
                    Query+="and (Charge >= '"+Charge+"')";
                else Query+="and (Charge <= '"+Charge+"')";

            if (!Age.equals(""))
                if (AgeType.equals("at least"))
                    Query+="and (DateDIFF(day,Birthday,getDate()) >="+Age+"*365)";
                else Query+="and (DateDIFF(day,Birthday,getDate()) <="+Age+"*365)";

            Query+="order by DIFFERENCE (Firstname, '"+searchString+"') desc";
            System.out.println(Query);
            re=st.executeQuery(Query);
            while (re.next())
            {
                addTableN();
            }

            this.jTableNanny.setModel(new DefaultTableModel (table,label));
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableNanny = new javax.swing.JTable(){
            public boolean isCellEditable(int rowIndex, int vColIndex)
            {
                return false;
            }
        }
        ;
        jLabel1 = new javax.swing.JLabel();
        jTextFieldSearch = new javax.swing.JTextField();
        jComboBoxSearch = new javax.swing.JComboBox();
        jButtonSearch = new javax.swing.JButton();
        jCheckBoxAdvancedSearch = new javax.swing.JCheckBox();
        jPanelAdvancedSearch = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldAge = new javax.swing.JTextField();
        jTextFieldCharge = new javax.swing.JTextField();
        jComboBoxCharge = new javax.swing.JComboBox();
        jComboBoxAge = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxGender = new javax.swing.JComboBox();
        jTextFieldExperience = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();

        setBackground(new java.awt.Color(227, 239, 255));
        setPreferredSize(new java.awt.Dimension(800, 400));

        jTableNanny.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "First Name", "Last Name", "AgeGroup", "Gender", "Address", "Status"
            }
        ));
        jTableNanny.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableNannyMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTableNanny);

        jLabel1.setText("Search: ");

        jComboBoxSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "NannyID", " ", " " }));

        jButtonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/Search1.png"))); // NOI18N
        jButtonSearch.setText("Search");
        jButtonSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchActionPerformed(evt);
            }
        });

        jCheckBoxAdvancedSearch.setBackground(new java.awt.Color(227, 239, 255));
        jCheckBoxAdvancedSearch.setText("     Advanced Search");
        jCheckBoxAdvancedSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxAdvancedSearchActionPerformed(evt);
            }
        });

        jPanelAdvancedSearch.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanelAdvancedSearch.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanelAdvancedSearch.setEnabled(false);
        jPanelAdvancedSearch.setFocusable(false);
        jPanelAdvancedSearch.setOpaque(false);
        jPanelAdvancedSearch.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setText("Age     :");
        jPanelAdvancedSearch.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel5.setText("Charge:");
        jPanelAdvancedSearch.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));
        jPanelAdvancedSearch.add(jTextFieldAge, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 170, -1));
        jPanelAdvancedSearch.add(jTextFieldCharge, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, 170, -1));

        jComboBoxCharge.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "at least", "at most" }));
        jPanelAdvancedSearch.add(jComboBoxCharge, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 40, 82, -1));

        jComboBoxAge.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "at least", "at most" }));
        jPanelAdvancedSearch.add(jComboBoxAge, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 10, 82, -1));

        jLabel3.setText("Gender      :");
        jPanelAdvancedSearch.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 10, -1, -1));

        jLabel4.setText("Experience:");
        jPanelAdvancedSearch.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(460, 40, -1, -1));

        jComboBoxGender.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Not Important", "Male", "Female" }));
        jPanelAdvancedSearch.add(jComboBoxGender, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 10, 160, -1));
        jPanelAdvancedSearch.add(jTextFieldExperience, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 40, 160, -1));

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/edit_1.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addGap(51, 51, 51)
                .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(55, 55, 55)
                .addComponent(jCheckBoxAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(190, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(76, 76, 76)
                .addComponent(jButtonSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
                .addGap(118, 118, 118)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(126, 126, 126)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(151, 151, 151))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jCheckBoxAdvancedSearch)))
                .addGap(18, 18, 18)
                .addComponent(jPanelAdvancedSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit)
                    .addComponent(btnAdd)
                    .addComponent(jButtonSearch)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
        // TODO add your handling code here:
        if (this.jCheckBoxAdvancedSearch.isSelected())
            advancedSearchN();
        else
            SearchN();
}//GEN-LAST:event_jButtonSearchActionPerformed

    private void jCheckBoxAdvancedSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxAdvancedSearchActionPerformed
        // TODO add your handling code here:
        boolean check = this.jCheckBoxAdvancedSearch.isSelected();
        if (check==true) {
            this.jPanelAdvancedSearch.setVisible(true);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
        } else {
            this.jPanelAdvancedSearch.setVisible(false);
            this.jComboBoxSearch.removeAllItems();
            this.jComboBoxSearch.addItem("Name");
            this.jComboBoxSearch.addItem("NannyID");
        }
}//GEN-LAST:event_jCheckBoxAdvancedSearchActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        AddNewNanny an = new AddNewNanny();
        an.setVisible(true);
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        int d=this.jTableNanny.getSelectedRow();// get selected row on table
        if(d>=0) {
            String idNanny=(String)this.jTableNanny.getValueAt(d, 0);
            EditNanny ed = new EditNanny(idNanny);
            ed.setVisible(true);
        }
}//GEN-LAST:event_btnEditActionPerformed

    private void jTableNannyMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableNannyMouseReleased
        // TODO add your handling code here:
        int selectedRow =  jTableNanny.getSelectedRow();
        if (evt.getClickCount()==2)
        {
            Integer NannyID = Integer.parseInt((String)jTableNanny.getValueAt(selectedRow, 0));
            detailNanny dn= new detailNanny(null,true,NannyID);
            dn.setVisible(true);
        }              
    }//GEN-LAST:event_jTableNannyMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton jButtonSearch;
    private javax.swing.JCheckBox jCheckBoxAdvancedSearch;
    private javax.swing.JComboBox jComboBoxAge;
    private javax.swing.JComboBox jComboBoxCharge;
    private javax.swing.JComboBox jComboBoxGender;
    private javax.swing.JComboBox jComboBoxSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanelAdvancedSearch;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableNanny;
    private javax.swing.JTextField jTextFieldAge;
    private javax.swing.JTextField jTextFieldCharge;
    private javax.swing.JTextField jTextFieldExperience;
    private javax.swing.JTextField jTextFieldSearch;
    // End of variables declaration//GEN-END:variables

}
